package yxy.game.pm2.command.plainsarena;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.ServerBean;
import yxy.game.pm2.bean.area.AreaBean;
import yxy.game.pm2.bean.module.rank.RankEntry;
import yxy.game.pm2.bean.module.rank.RankWorship;
import yxy.game.pm2.bean.module.rank.RankWorshipPlayer;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.PlainsArenaMessage;
import yxy.game.pm2.module.plainsarena.RankPlainsArena;
import yxy.game.pm2.module.player.PlayerHandle;

import java.util.Collection;

/** 中原擂台 中原擂台荣誉榜 */
@MessageType(proto = Command.PlainsArenaTop, classtype = PlainsArenaMessage.PlainsArenaTopRequest.class)
public class CMD_PlainsArenaTop extends Command<PlainsArenaMessage.PlainsArenaTopRequest> {

	@Override
	public void execute(MessageSource messageSource, PlainsArenaMessage.PlainsArenaTopRequest request) {
		String playerCode = messageSource.getTag();

		PlainsArenaMessage.PlainsArenaTopResponse.Builder response = PlainsArenaMessage.PlainsArenaTopResponse.newBuilder();

		PlayerBean playerBean = PlayerBean.get(playerCode);
		ServerBean serverBean = ServerBean.get(playerBean.getServerId());
		AreaBean areaBean = AreaBean.get(serverBean.getAreaId());

		PlayerHandle playerHandle = new PlayerHandle();
		// 5段前三
		{
			RankPlainsArena rankPlainsArena = new RankPlainsArena(areaBean.getGroupId(), 5);
			Collection<RankEntry> top = rankPlainsArena.top(3);

			RankWorship worship = rankPlainsArena.getWorship();
			RankWorshipPlayer worshipPlayer = RankWorshipPlayer.get(playerCode, worship);

			int rank = 0;
			for (RankEntry entry : top) {
				String pCode = entry.getKey();

				PlainsArenaMessage.PlainsArenaRank.Builder builder = PlainsArenaMessage.PlainsArenaRank.newBuilder();

				builder.setRank(++rank);
				builder.setLevel(5);
				builder.setPlayerInfo(playerHandle.playerSimpleInfo(pCode));
				builder.setFans(worship.getScore(pCode));
				builder.setLiked(worshipPlayer.getWorshiped().contains(pCode));
				response.addRank(builder);
			}
		}

		// 4段第一
		{
			RankPlainsArena rankPlainsArena = new RankPlainsArena(areaBean.getGroupId(), 4);
			Collection<RankEntry> top = rankPlainsArena.top(1);

			RankWorship worship = rankPlainsArena.getWorship();
			RankWorshipPlayer worshipPlayer = RankWorshipPlayer.get(playerCode, worship);

			int rank = 0;
			for (RankEntry entry : top) {
				String pCode = entry.getKey();

				PlainsArenaMessage.PlainsArenaRank.Builder builder = PlainsArenaMessage.PlainsArenaRank.newBuilder();

				builder.setRank(++rank);
				builder.setLevel(4);
				builder.setPlayerInfo(playerHandle.playerSimpleInfo(pCode));
				builder.setFans(worship.getScore(pCode));
				builder.setLiked(worshipPlayer.getWorshiped().contains(pCode));
				response.addRank(builder);
			}
		}
		// 3段第一
		{
			RankPlainsArena rankPlainsArena = new RankPlainsArena(areaBean.getGroupId(), 3);
			Collection<RankEntry> top = rankPlainsArena.top(1);

			RankWorship worship = rankPlainsArena.getWorship();
			RankWorshipPlayer worshipPlayer = RankWorshipPlayer.get(playerCode, worship);

			int rank = 0;
			for (RankEntry entry : top) {
				String pCode = entry.getKey();

				PlainsArenaMessage.PlainsArenaRank.Builder builder = PlainsArenaMessage.PlainsArenaRank.newBuilder();

				builder.setRank(++rank);
				builder.setLevel(3);
				builder.setPlayerInfo(playerHandle.playerSimpleInfo(pCode));
				builder.setFans(worship.getScore(pCode));
				builder.setLiked(worshipPlayer.getWorshiped().contains(pCode));
				response.addRank(builder);
			}
		}

		send(messageSource, response, Result.Success);
	}

	private void send(MessageSource messageSource, PlainsArenaMessage.PlainsArenaTopResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}